package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.maincardmanager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.DelCouponAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.bean.RedPageBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.HUD;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.view.WrapContentLinearLayoutManager;
import com.zzsy.carosshopprojects.view.dialog.ComTwoBtnDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 删除红包
 */
public class DelCouponFragment extends BaseFragment {
    public static DelCouponFragment newInstance(String uuid) {
        DelCouponFragment delCouponFragment = new DelCouponFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        delCouponFragment.setArguments(bundle);
        return delCouponFragment;
    }
    @BindView(R.id.tv_zong)
    TextView tvZong;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.btn_del)
    LinearLayout btnDel;
    @BindView(R.id.checkbox_all)
    CheckBox checkboxAll;
    @BindView(R.id.recyclerview)
    RecyclerView relDelView;
    private DelCouponAdapter delCouponAdapter;
    private ArrayList<RedPageBean> beanList;
    @BindView(R.id.ll_show)
    LinearLayout llShow;
    private String memberId;
    private ComTwoBtnDialog comTwnBtnDialog;
    @Override
    protected int initLayout() {
        return R.layout.fg_delcoupon;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.RedPageList), getActivity());
        btnDel.setOnClickListener(view1 -> {
            showDialogs();
        });
    }

    @Override
    protected void initData(Context mContext) {
        beanList = new ArrayList<>();
        delCouponAdapter = new DelCouponAdapter(getContext(), beanList,0, checkboxAll, tvZong, tvNum);
        relDelView.setLayoutManager(new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        relDelView.setAdapter(delCouponAdapter);
    }

    @Override
    protected void loadMethod() {
        getUUIDMemeberDataReplaceCard(getContext(),getArguments().getString("uuid"), DataUtil.getInstance().getCompanyId());
    }

    public void getUUIDMemeberDataReplaceCard(Context context, String uuid, String companyId) {
        RequestUtils.MemberByUuid(context, uuid, companyId, new MyObserver<MemberInfoBean>(context, false) {
            @Override
            public void onSuccess(MemberInfoBean bean) {
                memberId = bean.getMemberId();
                String productId = bean.getProductId();
                String memberCardId =bean.getMemberCardId();
                if (StringUtils.isEmpty(memberId)) {
                    ToastUtils.showToast(context.getApplicationContext(), "该会员异常！");
                    getActivity().finish();
                } else {
                    if (!StringUtils.isEmpty(productId)&& !StringUtils.isEmpty(memberCardId)) {
                        reqAllCourseList(memberId, productId,memberCardId);
                    }

                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }

    private void reqAllCourseList(String memberId, String productId,String memberCardId) {
        RequestUtils.GetMemberRedPackedList(getContext(), DataUtil.getInstance().getStoreId(), memberId, productId, memberCardId, new MyObserver<List<RedPageBean>>(getContext()) {
            @Override
            public void onSuccess(List<RedPageBean> result) {
                beanList.clear();
                if (result != null && result.size() > 0) {
                    for (int i = 0; i < result.size(); i++) {
                        if (!StringUtils.isEmpty(result.get(i).getCouponKind()) && "2".equals(result.get(i).getCouponKind())) {
                            beanList.add(result.get(i));
                        }
                    }
                    relDelView.setVisibility(View.VISIBLE);
                    llShow.setVisibility(View.GONE);
                } else {
                    relDelView.setVisibility(View.GONE);
                    llShow.setVisibility(View.VISIBLE);
                }
                delCouponAdapter.notifyDataSetChanged();
                if (delCouponAdapter != null) {
                    delCouponAdapter.checkAll_none(false);
                    delCouponAdapter.checkAll();
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });

    }

    /**
     * 删除优惠券
     */
    private void showDialogs() {
        if (comTwnBtnDialog != null) {
            comTwnBtnDialog.dismiss();
        }
        comTwnBtnDialog = new ComTwoBtnDialog(getActivity(), ComTwoBtnDialog.DEL_COUPON, null);
        comTwnBtnDialog.show();
        comTwnBtnDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                comTwnBtnDialog.dismiss();
            }

            @Override
            public void onDialogRightBtnClick() {
                relDelView.setVisibility(View.GONE);
                HUD.getInstance().show(getContext(), "正在删除红包...");
                String ids = "";
                for (int i = beanList.size(), j = 0; i > j; i--) {
                    RedPageBean listBean = beanList.get(i - 1);
                    if (listBean.isSelected()) {
                        beanList.remove(listBean);
                        ids = ids + "," + listBean.getId();
                    }
                }

                if (StringUtils.isEmpty(ids)) {
                    HUD.getInstance().dismiss();
                    relDelView.setVisibility(View.VISIBLE);
                    ToastUtils.showToast(context.getApplicationContext(), "请选择删除的红包！");
                    return;
                } else {
                    String subStr = ids.substring(1, ids.length());
                    ReceiptData(subStr);
                }

                comTwnBtnDialog.dismiss();
            }
        });
    }

    private void ReceiptData(String subStr) {
        RequestUtils.DeleteCoupon(getContext(), subStr, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {
                HUD.getInstance().dismiss();
               startWithPop(DelCouponSufFragment.newInstance());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                HUD.getInstance().dismiss();
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });


    }

}
